package com.class5.springboot.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.class5.springboot.entity.OrgUser;
import com.class5.springboot.entity.vo.Aup2ProjectOrgInfoApplyMgrVo;
import com.class5.springboot.frame.context.SuperServiceImpl;
import com.class5.springboot.frame.repo.Sql;
import com.class5.springboot.frame.repo.dto.PageData;
import com.class5.springboot.frame.repo.dto.ParamMap;
import com.class5.springboot.service.TestService;
import com.class5.springboot.util.Md5Util;
import com.class5.springboot.util.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service
public class TestServiceImpl extends SuperServiceImpl implements TestService{

    @Override
    @Transactional(readOnly = true)
    public PageData findOrgInfoApplyForPage(ParamMap param){
        String sql_user = "SELECT org.id,org.org_info_name orgInfoName,org.authorized_name authorizedName,org_sub.audit_name auditName,org_sub.audit_opinion auditOpinion,org_sub.audit_date auditDate, " +
                "org.authorized_phone authorizedPhone,org_sub.submit_date submitDate,org.audit_status auditStatus,org.project_type projectType, " +
                "CASE org_sub.accept_id WHEN NULL THEN '1' ELSE org_sub.accept_name END acceptStatus " +
                "FROM aup2_project_org_info_apply org ,aup2_project_org_info_apply_sub org_sub " +
                "where org.id = org_sub.org_info_apply_id ";
        String sql_admin = "SELECT org.id,org.org_info_name orgInfoName,org.authorized_name authorizedName,org_sub.audit_name auditName,org_sub.audit_opinion auditOpinion,org_sub.audit_date auditDate, " +
                "org.authorized_phone authorizedPhone,org_sub.submit_date submitDate,org.audit_status auditStatus,org.project_type projectType, " +
                "CASE org_sub.accept_id WHEN NULL THEN '1' ELSE org_sub.accept_name END acceptStatus " +
                "FROM aup2_project_org_info_apply org " +
                "LEFT JOIN aup2_project_org_info_apply_sub org_sub on org.id = org_sub.org_info_apply_id "+
                "where 1=1";
        Sql sql;
        if(param.get("userId")!=null){
            sql = Sql.get(sql_user);
            sql.and().eq("org_sub.accept_id",param.getLong("userId")).parent();
        }else{
            sql = Sql.get(sql_admin);
        }
        sql.and()
                .like("org.org_info_name",param.getString("orgInfoName"))
                .like("org.authorized_name",param.getString("authorizedName"))
                .like("org_sub.audit_name",param.getString("auditName"))
                .eq("org.aup2_project_id",param.getLong("projectId"))
                .eq("org.authorized_phone",param.getString("authorizedPhone"))
                .eq("org.audit_status",param.get("auditStatus"))
                .add("org.audit_status!=?","1")
                .eq("org.project_type",param.get("projectType")).parent();
        if(param.get("acceptStatus")!=null&&param.getString("acceptStatus").equals("2")){
            sql.and().isNotNull("org_sub.accept_id").parent();
        }else if(param.get("acceptStatus")!=null && param.getString("acceptStatus").equals("1")){
            sql.and().isNull("org_sub.accept_id").parent();
        }
        if(!StringUtils.isNull(param.getString("submitDate_start"))){
            sql.and().add("org_sub.submit_date >= ? ",param.get("submitDate_start")).parent();
        }
        if(!StringUtils.isNull(param.getString("submitDate_end"))){
            sql.and().add("org_sub.submit_date <= ? ",param.get("submitDate_end")).parent();
        }
        if(!StringUtils.isNull(param.getString("auditDate_start"))){
            sql.and().add("org_sub.audit_date >= ? ",param.get("auditDate_start")).parent();
        }
        if(!StringUtils.isNull(param.getString("auditDate_end"))){
            sql.and().add("org_sub.audit_date <= ? ",param.get("auditDate_end")).parent();
        }
        sql.orderBy("org_sub.audit_date","desc");
        PageData pageData = repo.queryForPage(sql,Aup2ProjectOrgInfoApplyMgrVo.class,param.getInt("page"),param.getInt("size"));
        return pageData;
    }

    @Override
    public void updateOrgUser(JSONObject param) {
        Long id = param.getLong("id");
        OrgUser orgUser = repo.get(id,OrgUser.class);
        orgUser.setEmail("tuguoba@163.com");
        repo.update(orgUser);
    }

    @Override
    public void insertOrgUser(JSONObject param) {
        OrgUser orgUser = new OrgUser();
        orgUser.setUserName("test");
        orgUser.setPassword(Md5Util.md5Hex("test"+"111111"));
        repo.insert(orgUser);
    }

    @Override
    @Transactional(readOnly = true)
    public PageData findOrgUserForPage(ParamMap param) {
        Sql sql = Sql.getSelect(OrgUser.class);
        PageData pageData = repo.queryForPage(sql,Aup2ProjectOrgInfoApplyMgrVo.class,param.getInt("page"),param.getInt("size"));
        return pageData;
    }

    public static void  main(String arg[]){
        System.out.println(Md5Util.md5Hex("admin@123"+"123456"));
    }
}
